package com.egene.tw.activity.settings;

import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.egene.tw.R;
import com.egene.tw.api.LoginApi;
import com.egene.tw.base.BaseActivity;
import com.egene.tw.dao.UserDao;
import com.egene.tw.model.net.ResponseBase;
import com.egene.tw.utils.CheckUtil;
import com.egene.tw.utils.JsonUtil;
import com.egene.tw.utils.MToast;
import com.loopj.android.http.AsyncHttpResponseHandler;

import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Bean;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;
import org.apache.http.Header;

@EActivity(R.layout.activity_change_phone)
public class ChangePhoneActivity extends BaseActivity {
    //ToolBar Views
    @ViewById(R.id.actionBar)
    Toolbar mToolbarView;
    @ViewById(R.id.toolbar_title)
    TextView mToolBarTitle;

    @ViewById
    EditText etPhone;
    @ViewById
    EditText etNewPhone;
    @ViewById
    EditText etVCode;
    @ViewById
    Button btnVCode;

    @Bean
    UserDao userDao;

    @AfterViews
    void initViews() {
        initToolBar();
    }

    private void initToolBar() {
        mToolBarTitle.setText(R.string.change_phone);
//        mToolbarView.setBackgroundResource(R.color.action_bar_bg);
        mToolbarView.setNavigationIcon(R.drawable.ic_back);
        mToolbarView.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    /**
     * 获取验证码
     */
    @Click(R.id.btnVCode)
    void getVCode() {
        if (!CheckUtil.checkPhone(etPhone.getText().toString())) {
            return;
        }
        if (!CheckUtil.checkPhone(etNewPhone.getText().toString())) {
            return;
        }
        etPhone.setEnabled(false);
        etNewPhone.setEnabled(false);
        btnVCode.setClickable(false);

        showWaitDialog(getString(R.string.get_v_code)).show();
        LoginApi.getVCode(etNewPhone.getText().toString(), "", new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                ResponseBase result = JsonUtil.parse(responseBody, ResponseBase.class);
                if (result != null) {
                    MToast.show(result.getMessage());
                }
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                MToast.show(R.string.no_network);
            }

            @Override
            public void onFinish() {
                hideWaitDialog();
                super.onFinish();
            }
        });
    }


    @Click(R.id.btnSubmit)
    void submit() {
        if (!CheckUtil.checkPhone(etPhone.getText().toString())) {
            return;
        }
        if (TextUtils.isEmpty(etVCode.getText())) {
            MToast.show(R.string.enter_v_code);
            return;
        }
//        mRegisterUser.setMobile(etPhone.getText().toString());

        showWaitDialog(getString(R.string.dealing)).show();


        LoginApi.changePhone(userDao.getUser().getUserID(),
                etPhone.getText().toString(),
                etNewPhone.getText().toString(),
                etVCode.getText().toString(),
                new AsyncHttpResponseHandler() {

                    @Override
                    public void onSuccess(int statusCode, Header[] headers, byte[] responseBody) {
                        ResponseBase result = JsonUtil.parse(responseBody, ResponseBase.class);
                        if (result != null) {
                            MToast.show(result.getMessage());
                            if (result.isSuccess()) {
                                setResult(RESULT_OK);
                                finish();
                            }
                        }
                    }

                    @Override
                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {
                        MToast.show(getString(R.string.no_network));
                    }

                    @Override
                    public void onFinish() {
                        hideWaitDialog();
                        super.onFinish();
                    }
                });
    }
}
